<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import the Joomla modellist library
jimport('joomla.application.component.modellist');
/**
 * Expert List Model
 */
class NrmResearchModelExperts extends JModelList
{
	/**
	 * Method to build an SQL query to load the list data.
	 *
	 * @return	string	An SQL query
	 */
public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
					'id', 'a.id',
					'year_of_publication', 'a.year_of_publication',
					'name', 'a.name',
			);
		}	
		parent::__construct($config);
	}
	protected function getListQuery()
	{	
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->select('a.*');
		$query->from('#__nrmresearch_experts as a');
		
		if ($this->getState('keywords')) {
			$query->where('a.keywords like "%'.$this->getState('keywords').'%"');
		}
		
		if ($this->getState('name')) {
			$query->where('a.name like "%'.$this->getState('name').'%"');
		}		
		if ($this->getState('areas')) {
			$query->where('a.areas like "%'.$this->getState('areas').'%"');
		}
	
		if ($this->getState('lead_organisation')) {			
			$query->where('a.organisation like "%'.$this->getState('lead_organisation').'%"');
		}
		
		if ($this->getState('division')) {
			$query->where('a.division like "%'.$this->getState('division').'%"');
		}

		//Add the list ordering clause.
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.id')).' '.$db->getEscaped($this->getState('list.direction', 'ASC')));		
		return $query;
	}
	protected function populateState($ordering = null, $direction = null)
	{
		// Initialise variables.
		$app	= JFactory::getApplication();
		$db		= $this->getDbo();

		$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', MAX_NUMS_ITEMS_PER_PAGE);		
		$this->setState('list.limit', $limit);
		$perpage = JRequest::getVar('limit', 0, '', 'int');		
		if($perpage != 0 )$this->setState('list.limit', $perpage);
	
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		$this->setState('list.start', $limitstart);
	
		$orderCol	= JRequest::getCmd('filter_order', 'id');
		if (!in_array($orderCol, $this->filter_fields)) {
			$orderCol = 'id';
		}
		$this->setState('list.ordering', $orderCol);
	
		$listOrder	=  JRequest::getCmd('filter_order_Dir', 'DESC');
		if (!in_array(strtoupper($listOrder), array('ASC', 'DESC', ''))) {
			$listOrder = 'ASC';
		}
		$this->setState('list.direction', $listOrder);
	
		$keywords = JRequest::getVar('keywords','','','string');
		if($keywords==KEYWORD_LABEL)$keywords = '';
		$this->setState('keywords',$keywords);
		
		$name = JRequest::getVar('expert_name','','','string');
		if($name == NAME_LABEL)$name = '';
		$this->setState('name',$name);
		
		$areas = JRequest::getVar('expert_areas','','','string');
		if($areas == AREAS_LABEL)$areas = '';
		$this->setState('areas',$areas);
		
		$organisation = JRequest::getVar('expert_lead_organisation','','','string');	
		if($organisation == LEAD_LABEL)$organisation = '';
		$this->setState('lead_organisation',$organisation);
		
		$division = JRequest::getVar('expert_division','','','string');
		if($division == DIVISION_LABEL)$division = '';
		$this->setState('division',$division);
	}
}
